﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DataComminucation;
using System.Xml;
using Picking.Common;
using System.Xml.Schema;

namespace XmlDataComminucation
{
    public class XmlParametersSetupDataAccess : IParametersSetupDataAccess
    {
        public DataRow GetInUseParameters()
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNamespaceManager nsmgr = GeneralFunctions.GetXmlNamespaceMgr();
            XmlNode node = doc.SelectSingleNode("//x:ParametersSetup/x:Record[x:IsUse='T']", nsmgr);
            if (node == null) return null;
            else return GeneralFunctions.GetDataRowByXmlNode(node);
        }

        /// <summary>
        /// Get Config Setup
        /// </summary>
        /// <returns></returns>
        public DataTable GetConfigSetup()
        {
            DataTable result = null;

            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNamespaceManager nsmgr = GeneralFunctions.GetXmlNamespaceMgr();
            XmlNodeList nodes = doc.SelectNodes("//x:ParametersSetup/x:Record", nsmgr);

            if (nodes != null && nodes.Count > 0)
            {
                result = GeneralFunctions.GetDataTableByXmlNodes(nodes);
            }

            if (result == null)
            {
                result = GeneralFunctions.CreateDataTableFromXmlSchema("ParametersSetup");
            }

            return result;
        }

        /// <summary>
        /// Save Config Setup
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public bool SaveConfigSetup(DataTable table)
        {
            try
            {
                XmlDocument doc = GeneralFunctions.GetXmlDocument();

                GeneralFunctions.RemoveAllRecordsInTable(doc, "ParametersSetup");

                foreach (DataRow row in table.Rows)
                {
                    Dictionary<string, string> columnNamesAndValues = new Dictionary<string, string>();

                    foreach (DataColumn col in table.Columns)
                    {
                        columnNamesAndValues.Add(col.ColumnName, row[col.ColumnName].ToString());
                    }

                    GeneralFunctions.CreateRecordInTable(doc, "ParametersSetup", columnNamesAndValues);

                }

                GeneralFunctions.SaveXmlDocument(doc);
            }
            catch (Exception ex)
            {
                throw (ex);
                //return false;
            }

            return true;
        }

        /// <summary>
        /// Get Workers And Lights Setup
        /// </summary>
        /// <returns></returns>
        public WorkersAndLightsSetupModel GetWorkersAndLightsSetup()
        {
            WorkersAndLightsSetupModel model = new WorkersAndLightsSetupModel();
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNamespaceManager nsmgr = GeneralFunctions.GetXmlNamespaceMgr();
            XmlNodeList nodes = doc.SelectNodes("//x:WorkersAndLightsSetup/x:Record[x:Picking_Worker_No > 0]", nsmgr);

            if (nodes != null && nodes.Count > 0)
            {
                model.WorkersAndLightsSetupTable = GeneralFunctions.GetDataTableByXmlNodes(nodes);
            }

            if (model.WorkersAndLightsSetupTable == null)
            {
                model.WorkersAndLightsSetupTable = GeneralFunctions.CreateDataTableFromXmlSchema("WorkersAndLightsSetup");
            }

            return model;
        }

        /// <summary>
        /// Save Workers And Lights Setup
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveWorkersAndLightsSetup(WorkersAndLightsSetupModel model)
        {
            try
            {
                XmlDocument doc = GeneralFunctions.GetXmlDocument();
                XmlNamespaceManager nsmgr = GeneralFunctions.GetXmlNamespaceMgr();

                GeneralFunctions.RemoveAllRecordsInTable(doc, "WorkersAndLightsSetup");

                foreach (DataRow row in model.WorkersAndLightsSetupTableCopy.Rows)
                {
                    Dictionary<string, string> columnNamesAndValues = new Dictionary<string, string>();

                    foreach (DataColumn col in model.WorkersAndLightsSetupTableCopy.Columns)
                    {
                        columnNamesAndValues.Add(col.ColumnName, row[col.ColumnName].ToString());
                    }

                    GeneralFunctions.CreateRecordInTable(doc, "WorkersAndLightsSetup", columnNamesAndValues);
                }

                GeneralFunctions.SaveXmlDocument(doc);
            }
            catch (Exception ex)
            {
                throw (ex);
                //return false;
            }

            return true;
        }

        /// <summary>
        /// GetLight And DNo Setup
        /// </summary>
        /// <returns></returns>
        public LightAndDNoSetupModel GetLightAndDNoSetup()
        {
            LightAndDNoSetupModel model = new LightAndDNoSetupModel();
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNamespaceManager nsmgr = GeneralFunctions.GetXmlNamespaceMgr();
            XmlNodeList nodes = doc.SelectNodes("//x:LightAndDNoSetup/x:Record[x:Light_No > 0]", nsmgr);

            if (nodes != null && nodes.Count > 0)
            {
                model.LightAndDNoSetupTable = GeneralFunctions.GetDataTableByXmlNodes(nodes);
            }

            if (model.LightAndDNoSetupTable == null)
            {
                model.LightAndDNoSetupTable = GeneralFunctions.CreateDataTableFromXmlSchema("LightAndDNoSetup");
            }

            return model;
        }

        /// <summary>
        /// Save Light And DNo Setup
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveLightAndDNoSetup(LightAndDNoSetupModel model)
        {
            try
            {
                XmlDocument doc = GeneralFunctions.GetXmlDocument();

                GeneralFunctions.RemoveAllRecordsInTable(doc, "LightAndDNoSetup");

                foreach (DataRow row in model.LightAndDNoSetupTableCopy.Rows)
                {
                    Dictionary<string, string> columnNamesAndValues = new Dictionary<string, string>();

                    foreach (DataColumn col in model.LightAndDNoSetupTableCopy.Columns)
                    {
                        columnNamesAndValues.Add(col.ColumnName, row[col.ColumnName].ToString());
                    }

                    GeneralFunctions.CreateRecordInTable(doc, "LightAndDNoSetup", columnNamesAndValues);
                }

                GeneralFunctions.SaveXmlDocument(doc);
            }
            catch (Exception ex)
            {
                throw (ex);
                //return false;
            }

            return true;
        }

        /// <summary>
        /// Get Sebango And DNo Setup
        /// </summary>
        /// <returns></returns>
        public SebangoAndDNoSetupModel GetSebangoAndDNoSetup()
        {
            SebangoAndDNoSetupModel model = new SebangoAndDNoSetupModel();
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNamespaceManager nsmgr = GeneralFunctions.GetXmlNamespaceMgr();
            XmlNodeList nodes = doc.SelectNodes("//x:SebangoAndMachineSetup/x:Record[x:Sebango_ID > 0]", nsmgr);

            if (nodes != null && nodes.Count > 0)
            {
                model.SebangoAndMachineSetupTable = GeneralFunctions.GetDataTableByXmlNodes(nodes);
            }

            if (model.SebangoAndMachineSetupTable == null)
            {
                model.SebangoAndMachineSetupTable = GeneralFunctions.CreateDataTableFromXmlSchema("SebangoAndMachineSetup");
            }


            XmlNodeList nodesSebangoAndDNo = doc.SelectNodes("//x:SebangoAndDNoSetup/x:Record[x:Sebango_ID > 0]", nsmgr);

            if (nodesSebangoAndDNo != null && nodesSebangoAndDNo.Count > 0)
            {
                model.SebangoAndDNoSetupTable = GeneralFunctions.GetDataTableByXmlNodes(nodesSebangoAndDNo);
            }

            if (model.SebangoAndDNoSetupTable == null)
            {
                model.SebangoAndDNoSetupTable = GeneralFunctions.CreateDataTableFromXmlSchema("SebangoAndDNoSetup");
            }

            return model;
        }

        /// <summary>
        /// Save Sebango And DNo Setup
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveSebangoAndDNoSetup(SebangoAndDNoSetupModel model)
        {
            try
            {
                XmlDocument doc = GeneralFunctions.GetXmlDocument();

                GeneralFunctions.RemoveAllRecordsInTable(doc, "SebangoAndMachineSetup");

                foreach (DataRow row in model.SebangoAndMachineSetupTable.Rows)
                {
                    Dictionary<string, string> columnNamesAndValues = new Dictionary<string, string>();

                    foreach (DataColumn col in model.SebangoAndMachineSetupTable.Columns)
                    {
                        columnNamesAndValues.Add(col.ColumnName, row[col.ColumnName].ToString());
                    }

                    GeneralFunctions.CreateRecordInTable(doc, "SebangoAndMachineSetup", columnNamesAndValues);
                }


                GeneralFunctions.RemoveAllRecordsInTable(doc, "SebangoAndDNoSetup");

                foreach (DataRow row in model.SebangoAndDNoSetupTable.Rows)
                {
                    Dictionary<string, string> columnNamesAndValues = new Dictionary<string, string>();

                    foreach (DataColumn col in model.SebangoAndDNoSetupTable.Columns)
                    {
                        columnNamesAndValues.Add(col.ColumnName, row[col.ColumnName].ToString());
                    }

                    GeneralFunctions.CreateRecordInTable(doc, "SebangoAndDNoSetup", columnNamesAndValues);
                }

                GeneralFunctions.SaveXmlDocument(doc);
            }
            catch (Exception ex)
            {
                throw (ex);
                //return false;
            }

            return true;
        }
    }
}
